<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Financial Report</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>


<!-- <link rel="stylesheet" href="css/main.css"> -->


<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>


<script type="text/javascript" language="javascript" src="js/prototype-1.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-base-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-date-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/behaviour.js"></script>
<script type="text/javascript" language="javascript" src="js/datepicker.js"></script>
<link rel="stylesheet" href="css/datepicker.css"/>


<script type="text/javascript" language="javascript" src="js/behaviors.js"></script>

<script type="text/javascript" language="javascript" src="js/jquery-ui-timepicker-addon.js"></script>

<script type="text/javascript">

	
	function printFinancialReport(){
		document.getElementById("headerTitle").style.display = "none";
		document.getElementById("header").style.display = "none";
		document.getElementById("nav").style.display = "none";
		document.getElementById("printButtonDiv").style.display = "none";
		document.getElementById("footer").style.display = "none";search
		document.getElementById("printButtonDiv").style.display = "none";
		document.getElementById("search").style.display = "none";
		document.getElementById("dataListDiv").style["max-height"] = "none";
		//document.getElementById("dataListDiv").style.;
		window.print();
		document.getElementById("headerTitle").style.display = "block";
		document.getElementById("header").style.display = "block";
		document.getElementById("nav").style.display = "block";
		document.getElementById("printButtonDiv").style.display = "block";
		document.getElementById("footer").style.display = "block";
		document.getElementById("printButtonDiv").style.display = "block";
		document.getElementById("search").style.display = "block";
		document.getElementById("dataListDiv").style["max-height"] = "600px";
		//document.getElementById("dataListDiv").style.setPropertyValue("max-height", "600px");
	}



	function previousPage(){
		window.location.href = "index.php";
	}


	function FormSubmit(){
		var startDateObject = document.getElementById("startDate");
		var endDateObject = document.getElementById("endDate");
		var startDate = startDateObject.value;
		var endDate = endDateObject.value;


		if( startDate == "" && endDate == "" ){
			document.getElementById("errorMsg").innerHTML = "please input date!!!";
			startDateObject.focus();
			return;
		}
		
		var startMill = 0;
		var endMill = 0;

		if( startDate != "" ){
			var startDateArray = startDate.split("/");
			startDate = startDateArray[1] + "/" + startDateArray[0] + "/" + startDateArray[2];
			var startDateObject = new Date(startDate);
			startMill = startDateObject.getTime();
		}

		if( endDate == "" ){
			var endDateObject = new Date();
			endMill = endDateObject.getTime();
		}else{
			var endDateArray = endDate.split("/");
			endDate = endDateArray[1] + "/" + endDateArray[0] + "/" + endDateArray[2];
			var endDateObject = new Date(endDate);
			endMill = endDateObject.getTime();
		}

		if( startMill > endMill ){
			document.getElementById("errorMsg").innerHTML = "end should be after the start!";
			startDateObject.focus();
			return;
		}

		document.getElementById("financialReport").submit();
		
	}


	jQuery(document).ready(function(){
		var datepicker_CurrentInput;  
        jQuery.datepicker.setDefaults({ showButtonPanel: true, closeText: 'clear', beforeShow: function (input, inst) { datepicker_CurrentInput = input; } });  
        jQuery(".ui-datepicker-close").live("click", function (){  
            datepicker_CurrentInput.value = "";  
        });  
		
		jQuery("#startDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
		jQuery("#endDate").datepicker({
			dateFormat:'dd/mm/yy',
			changeMonth:true,
			changeYear:true,
			yearRange:'1910:2043'
		});
	});
</script>
</head>

<body>
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php 
			
			$ini_array = parse_ini_file("config/config.ini");
			
			date_default_timezone_set($ini_array["default_timezone"]);
			
			$resultNumber = 0;
			$startDate = "";
			$endDate = "";
			$reportResult;
			
			$paymentMethod = "";
			if( isset($_POST["paymentMethod"])){
				$paymentMethod = $_POST["paymentMethod"];
			}
			
			if( isset($_POST["startDate"]) || isset($_POST["endDate"]) ){
				if( isset($_POST["startDate"]) && strcmp($_POST["startDate"],"") != 0){
					$startDate = $_POST["startDate"];
				}else{
					$startDate = "01/01/1970";
				}
				
				if( isset($_POST["endDate"]) &&  strcmp($_POST["endDate"],"") != 0 ){
					$endDate = $_POST["endDate"];
				}else{
					$endDate = date("d/m/Y");

				}
				$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the ");
				mysqli_set_charset($conn, "utf8");
				ini_set('default_charset', "UTF-8");
				$startDate = mysqli_real_escape_string($conn,$startDate);
				$endDate = mysqli_real_escape_string($conn, $endDate);
				
				
				
				
				$getReportSql = "SELECT appointment_no, patient_no, datetime_patient_seen, professional_fee, retention_tax, payment_method, clinic_name FROM visit left outer join clinic
				on visit.`Clinic_no`=clinic.`Clinic_no` where datetime_patient_seen > STR_TO_DATE('".$startDate."','%d/%m/%Y') AND datetime_patient_seen < STR_TO_DATE('".$endDate."','%d/%m/%Y')";
				
				if( strcmp($paymentMethod,"") != 0 ){	
					$getReportSql .= "  and payment_method='".$paymentMethod."'";
				}
				$reportResult = mysqli_query($conn, $getReportSql);
				$resultNumber = mysqli_num_rows($reportResult);

			}else{
				$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the ");
				$currentDate = date("Y-m");
				$getReportSql = "SELECT appointment_no, patient_no, datetime_patient_seen, professional_fee, retention_tax, payment_method, clinic_name FROM visit left outer join clinic
					on visit.`Clinic_no`=clinic.`Clinic_no` where date_format(datetime_patient_seen,'%Y-%m')='".$currentDate."'";
				
				if( strcmp($paymentMethod,"") != 0  ){
					$getReportSql .= "  and payment_method='".$paymentMethod."'";
				}
				$reportResult = mysqli_query($conn, $getReportSql);
				$resultNumber = mysqli_num_rows($reportResult);
				
			}
			
			
			
			
		?>
		
		<div id="search">
			<form  name="form1" method="post" action="financial_report.php" id="financialReport">
				<label>Start Date:</label><input type="text" name="startDate" id="startDate" value="<?php echo $startDate?>"  readonly="readonly"/>
				<label>End Date:</label><input type="text" name="endDate" id="endDate" value="<?php echo $endDate?>" readonly="readonly"/>
				<label>Payment Method:</label>
				<select name="paymentMethod" id="paymentMethod" onchange="changeFunc()">
					<option value=""></option>
					<option value="VHI" <?php if( strcasecmp("VHI", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>VHI</option>
					<option value="Aviva" <?php if( strcasecmp("Aviva", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Aviva</option>
					<option value="Cash" <?php if( strcasecmp("Cash", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Cash</option>
					<option value="Cheque" <?php if( strcasecmp("Cheque", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Cheque</option>
					<option value="Quinn" <?php if( strcasecmp("Quinn", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Quinn</option>
					<option value="Bo Fee" <?php if( strcasecmp("Bo Fee", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Bo Fee</option>
					<option value="Bupa" <?php if( strcasecmp("Bupa", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Bupa</option>
					<option value="ESB" <?php if( strcasecmp("Quinn", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>ESB</option>
					<option value="Garda" <?php if( strcasecmp("Quinn", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Garda</option>
					<option value="No Fee" <?php if( strcasecmp("No Fee", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>No Fee</option>
					<option value="Paid" <?php if( strcasecmp("Paid", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Paid</option>
					<option value="Unpaid" <?php if( strcasecmp("Unpaid", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Unpaid</option>
					<option value="Other" <?php if( strcasecmp("Other", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Other</option>	
				</select>
				<input type="button" name="generate" value="generate" onclick="FormSubmit()" />
				<label id="errorMsg"></label>
			</form>
			
		</div>
		<div id="main_content">
			<div>
				<div ><h1>Fees</h1></div>
				<div><label><?php echo date("d-m-Y")?></label></div>
			</div>
			<div id="tableHead" style="margin:10px auto;width:980px;">
				<div style="width:110px;display:inline-block;"><label>PatientsNo.</label></div>
				<div style="width:130px;display:inline-block;"><label>DatePatientSeen</label></div>
				<div style="width:140px;display:inline-block;"><label>ProfessionalFee</label></div>
				<div style="width:260px;display:inline-block;"><label>PaymentMethod</label></div>
				<div style="width:100px;display:inline-block;text-align:center;"><label>Clinic</label></div>
			</div>
			
			<div style="max-height:600px;overflow:auto;width:980px;margin:10px auto;" id="dataListDiv">
				<table style="width:960px;">
					<?php 
						$totalFee = 0;
						$totalTax = 0;
						if( $resultNumber != 0 ){
							while( $row = mysqli_fetch_array($reportResult)){
								$appointmentNo = $row["appointment_no"];
								$patientNo = $row["patient_no"];
								$datetimeSeen = date("d/m/Y", strtotime($row["datetime_patient_seen"]));
								$professionalFee = $row["professional_fee"];
								$retensionalTax = $row["retention_tax"];
								$paymentMethod = $row["payment_method"];
								$clinicName = $row["clinic_name"];
								$totalFee += $professionalFee;
								$totalTax += $retensionalTax;
		
					?>
						<tr>
							<td style="text-align:left;width:12%;"><label><?php echo $patientNo?></label></td>
							<td style="text-align:left;width:18%;"><label><?php echo $datetimeSeen?></label></td>
							<td style="text-align:left;width:12%;"><label><?php echo $professionalFee?></label></td>
							<td style="text-align:left;width:12%;"><label><?php echo $paymentMethod?></label></td>
							<td style="text-align:right;width:48%;"><label><?php echo $clinicName?></label></td>
						</tr>
					<?php 
							}
						}
					?>
				</table>
			</div>
			<div style="text-align:center;margin:15px 0px;display:none;"><label>Monthly Total:</label><label><?php echo round($totalFee, 2)?></label></div>
			<div style="text-align:center;margin:15px 0px;"><label>Total Tax:</label><label><?php echo round($totalTax, 2)?></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>Total Fee:</label><label><?php echo round($totalFee, 2)?></label></div>
			<div id="printButtonDiv" style="text-align:center;margin:40px auto;">
				<input type="button" value="Print Report" onclick="printFinancialReport()"/>
				<input type="button" value="return" onclick="previousPage()" />
			</div>
		</div>
		<?php include_once 'footer.php';?>
	</div>
</body>
</html>
